<?php

/**
 * @file
 * Contains page callbacks and theme functions for the frontend UI.
 */

/**
 * Page callback for getting autocomplete suggestions.
 */
function search_api_autocomplete_autocomplete(SearchApiAutocompleteSearch $search, $keys = '') {
  $ret = array();
  if ($search->supportsAutocompletion()) {
    $server = $search->server();
    list($complete, $incomplete) = $search->splitKeys($keys);
    $keys = preg_replace('/\s+/', ' ', trim($keys));
    $query = $search->getQuery($complete, $incomplete);
    if ($query) {
      // @todo Maybe make range configurable?
      $query->range(0, 10);
      $query->setOption('search id', 'search_api_autocomplete');
      $query->preExecute();
      $suggestions = $server->getAutocompleteSuggestions($query, $search, $incomplete, $keys);
      if ($suggestions) {
        foreach ($suggestions as $suggestion) {
          // Convert suggestion strings into an array.
          if (is_string($suggestion)) {
            $pos = strpos($suggestion, $keys);
            if ($pos === FALSE) {
              $suggestion = array(
                'user_input' => '',
                'suggestion_suffix' => $suggestion,
              );
            }
            else {
              $suggestion = array(
                'suggestion_prefix' => substr($suggestion, 0, $pos),
                'user_input' => $keys,
                'suggestion_suffix' => substr($suggestion, $pos + strlen($keys)),
              );
            }
          }
          // Add defaults.
          $suggestion += array(
            'prefix' => NULL,
            'suggestion_prefix' => '',
            'user_input' => $keys,
            'suggestion_suffix' => '',
            'results' => NULL,
          );
          if (empty($search->options['results'])) {
            unset($suggestion['results']);
          }
          $key = $suggestion['suggestion_prefix'] . $suggestion['user_input'] . $suggestion['suggestion_suffix'];
          if (!isset($ret[$key])) {
            $ret[$key] = theme('search_api_autocomplete_suggestion', $suggestion);
          }
        }
      }
    }
  }
  drupal_json_output($ret);
}

/**
 *
 *
 * @param array $variables
 *   An associative array containing:
 *   - prefix: For special suggestions, some kind of prefix describing them.
 *   - suggestion_prefix: A suggested prefix for the entered input.
 *   - user_input: The input entered by the user.
 *   - suggestion_suffix: A suggested suffix for the entered input.
 *   - results: If available, the estimated number of results for these keys.
 */
function theme_search_api_autocomplete_suggestion(array $variables) {
  extract($variables);
  $output = '';
  if ($prefix) {
    $output .= "<span class=\"autocomplete-suggestion-note\">$prefix</span> ";
  }
  if ($suggestion_prefix) {
    $output .= "<span class=\"autocomplete-suggestion-prefix\">$suggestion_prefix</span>";
  }
  if ($user_input) {
    $output .= "<span class=\"autocomplete-user-input\">$user_input</span>";
  }
  if ($suggestion_suffix) {
    $output .= "<span class=\"autocomplete-suggestion-suffix\">$suggestion_suffix</span>";
  }
  if ($results) {
    $output .= " <span class=\"autocomplete-suggestion-results\">$results</span>";
  }

  return "<div class=\"search-api-autocomplete-suggestion\">$output</div>";
}
